package com.gamotech.schoollifedemo.channel.common

import android.content.Context
import android.content.Intent
import com.gamotech.schoollifedemo.utils.EncryptUtils
import com.gamotech.schoollifedemo.utils.SharedPreferenceUtils
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel

class CommonChannel private constructor(context: Context) : MethodChannel.MethodCallHandler {
    private var mContext: Context = context
    private var commonMethodChannel: MethodChannel? = null

    companion object {
        fun createCommonChannel(context: Context): CommonChannel {
            return CommonChannel(context)
        }
    }

    fun register(binaryMessenger: BinaryMessenger) {
        commonMethodChannel = MethodChannel(binaryMessenger, CommonContract.name)
        commonMethodChannel?.setMethodCallHandler(this)
    }

    object CommonContract {
        const val name = "com.commonchannel"

        const val photoSelector = "photoSelector"
        const val showAlertDialog = "showAlertDialog"
        const val getAppVersion = "getAppVersion"
        const val setAppVersion = "setAppVersion"
        const val convertSHA256 = "convertSHA256" // object转换为sha256
        const val convertMD5 = "convertMD5" // object转换为sha256
        const val encodeBase64 = "encodeBase64" // 转为base64
        const val decodeBase64 = "decodeBase64"//base64解码

        // 保存到sharedPreference中
        const val saveToSharedPreferenceString = "saveToSharedPreferenceString"
        const val saveToSharedPreferenceInt = "saveToSharedPreferenceInt"
        const val saveToSharedPreferenceBool = "saveToSharedPreferenceBool"
        const val saveToSharedPreferenceFloat = "saveToSharedPreferenceFloat"
        const val saveToSharedPreferenceLong = "saveToSharedPreferenceLong"

        // 从sharedPreference中获取内容
        const val getFromSharedPreferenceString = "getFromSharedPreferenceString"
        const val getFromSharedPreferenceInt = "getFromSharedPreferenceInt"
        const val getFromSharedPreferenceBool = "getFromSharedPreferenceBool"
        const val getFromSharedPreferenceFloat = "getFromSharedPreferenceFloat"
        const val getFromSharedPreferenceLong = "getFromSharedPreferenceLong"

        // 信息监听服务
        const val startMessageServerListen = "startMessageServerListen" // 开启服务
        const val stopMessageServerListen = "stopMessageServerListen" // 关闭服务

        const val sendMessage = "sendMessage" // 发送信息

        // 网站解析

    }

    override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
        when (call.method) {
            CommonContract.photoSelector -> {

            }

            CommonContract.showAlertDialog -> {

            }
            CommonContract.setAppVersion -> {
                // 设置app版本到sharedPreference中
                val param = call.argument<String>("version")
                param?.let {
                    val utils = SharedPreferenceUtils.createSharedPreferenceUtils(mContext)
                    utils.putValue("version", param)
                }
            }
            CommonContract.getAppVersion -> {
                // 获取app版本并且返回结果
                val param = call.argument<String>("version")
                param?.let {
                    val utils = SharedPreferenceUtils.createSharedPreferenceUtils(mContext)
                    val version = utils.getValue(it, "0.0.0")
                    result.success(version)
                }
            }
            CommonContract.convertSHA256 -> {
                val any: String? = call.argument<String?>("object")
                if (any == null) {
                    result.success("")
                    return
                }
                val encryptUtils = EncryptUtils()
                encryptUtils.init()
                result.success(encryptUtils.convert2SHA256(any))
                encryptUtils.dispose()
            }
            CommonContract.convertMD5 -> {
                val any: String? = call.argument<String?>("object")
                if (any == null) {
                    result.success("")
                    return
                }
                val encryptUtils = EncryptUtils()
                encryptUtils.init()
                result.success(encryptUtils.convert2MD5(any))
                encryptUtils.dispose()
            }
            CommonContract.saveToSharedPreferenceString -> {
                val key = call.argument<String?>("key") ?: return
                val value = call.argument<String?>("value") ?: return
                val utils = SharedPreferenceUtils.createSharedPreferenceUtils(mContext)
                utils.putValue(key, value)
            }
            CommonContract.saveToSharedPreferenceInt -> {
                val key = call.argument<String?>("key") ?: return
                val value = call.argument<Int?>("value") ?: return
                val utils = SharedPreferenceUtils.createSharedPreferenceUtils(mContext)
                utils.putValue(key, value)
            }
            CommonContract.saveToSharedPreferenceBool -> {
                val key = call.argument<String?>("key") ?: return
                val value = call.argument<Boolean?>("value") ?: return
                val utils = SharedPreferenceUtils.createSharedPreferenceUtils(mContext)
                utils.putValue(key, value)
            }
            CommonContract.saveToSharedPreferenceLong -> {
                val key = call.argument<String?>("key") ?: return
                val value = call.argument<Long?>("value") ?: return
                val utils = SharedPreferenceUtils.createSharedPreferenceUtils(mContext)
                utils.putValue(key, value)
            }
            CommonContract.saveToSharedPreferenceFloat -> {
                val key = call.argument<String?>("key") ?: return
                val value = call.argument<Float?>("value") ?: return
                val utils = SharedPreferenceUtils.createSharedPreferenceUtils(mContext)
                utils.putValue(key, value)
            }
            CommonContract.getFromSharedPreferenceBool -> {
                val key = call.argument<String?>("key") ?: return
                val defaultValue = call.argument<Boolean?>("defaultValue") ?: return
                val utils = SharedPreferenceUtils.createSharedPreferenceUtils(mContext)

                result.success(utils.getValue(key, defaultValue))
            }
            CommonContract.getFromSharedPreferenceInt -> {
                val key = call.argument<String?>("key") ?: return
                val defaultValue = call.argument<Int?>("defaultValue") ?: return
                val utils = SharedPreferenceUtils.createSharedPreferenceUtils(mContext)
                result.success(utils.getValue(key, defaultValue))
            }
            CommonContract.getFromSharedPreferenceLong -> {
                val key = call.argument<String?>("key") ?: return
                val defaultValue = call.argument<Long?>("defaultValue") ?: return
                val utils = SharedPreferenceUtils.createSharedPreferenceUtils(mContext)
                result.success(utils.getValue(key, defaultValue))
            }
            CommonContract.getFromSharedPreferenceString -> {
                val key = call.argument<String?>("key") ?: return
                val defaultValue = call.argument<String?>("defaultValue") ?: return
                val utils = SharedPreferenceUtils.createSharedPreferenceUtils(mContext)
                result.success(utils.getValue(key, defaultValue))
            }
            CommonContract.getFromSharedPreferenceFloat -> {
                val key = call.argument<String?>("key") ?: return
                val defaultValue = call.argument<Float?>("defaultValue") ?: return
                val utils = SharedPreferenceUtils.createSharedPreferenceUtils(mContext)
                result.success(utils.getValue(key, defaultValue))
            }
            CommonContract.startMessageServerListen -> {
                val service = Class.forName("com.gamotech.communication.connect.impl.UdpService")
                mContext.startService(Intent(mContext, service))
            }
            CommonContract.stopMessageServerListen -> {

            }
            CommonContract.sendMessage -> {
                // 发送信息
                val msg = call.argument<String?>("msg") ?: return
                val sourceAccount = call.argument<String?>("sourceAccount") ?: return
                val targetAccount = call.argument<String?>("targetAccount") ?: return

            }
            CommonContract.encodeBase64 -> {
                val any: String? = call.argument<String?>("object")
                val encryptUtils = EncryptUtils()
                encryptUtils.init()
                result.success(encryptUtils.encodeBase64(any))
                encryptUtils.dispose()
            }
            CommonContract.decodeBase64 -> {
                val any: String? = call.argument<String?>("object")
                val encryptUtils = EncryptUtils()
                encryptUtils.init()
                result.success(encryptUtils.decodeBase64(any))
                encryptUtils.dispose()
            }
        }
    }


}

